package ypmits.cms.view.windows {
	import ypmits.cms.view.components.CMSTextInput;
	import ypmits.cms.event.LoginEvent;
	import ypmits.cms.view.components.CMSButtonWhite;
	import ypmits.components.util.TextFieldUtility;
	import ypmits.font.Arial;
	import ypmits.setup.EasySetup;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * @author Dave Lenz
	 */
	public class Login extends Sprite {
		
		private static var _instance : Login;

		
		public static function get instance() : Login {
			if( !_instance ) _instance = new Login( new SingletonEnforcer( ) );
			return _instance;
		}
		
		
		public function Login ( enforcer : SingletonEnforcer ) {
			if (!enforcer) trace( "Class must be instantiated using the getInstance method" );
			
			draw();
		}
		
		private function draw() : void {
			var back : Back = new Back();
			
			var title : TextFieldUtility = new TextFieldUtility( "Log In", new Arial().fontName, 35, 0xFFFFFF );
			
			var naamfield : CMSTextInput = new CMSTextInput( true );
			naamfield.x = 5;
			naamfield.y = 76;
			naamfield.titlefield.text = "naam";
			naamfield.invullerfield.text = "";
			
			var wwfield : CMSTextInput = new CMSTextInput( true, true );
			wwfield.x = 5;
			wwfield.y = 146;
			wwfield.titlefield.text = "naam";
			wwfield.invullerfield.text = "";

			var sendbutton : CMSButtonWhite = new CMSButtonWhite( "OK", StageAlign.RIGHT );
			sendbutton.addEventListener( MouseEvent.CLICK, sendHandler );

			addChild( back );
			addChild( title );
			addChild( naamfield );
			addChild( sendbutton );
			addChild( wwfield );
			
			var r : Rectangle = this.getBounds(this);
			r.x = -(r.width * .5);
			r.y = -(r.height * .5);
		}
		
		
		private function sendHandler(event : MouseEvent) : void {
			var ok : Boolean = true;
			
			if( ok ) {
				SectionChooser.instance.show();
				hide();
				EasySetup.instance.loginCMItem.caption = "Show CMS";
				
				dispatchEvent( new LoginEvent( LoginEvent.LOGIN_OK ) );
			}
		}
		
		
		public function show() : void {
			alpha = 0;
			visible = false;
		}
		
		
		public function hide() : void {
			alpha = 1;
			visible = true;
		}
				
	}
}

class SingletonEnforcer {}